package com.mars.foodserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.foodserver.entity.FoodComment;
import com.mars.foodserver.entity.NotesComment;
import com.mars.foodserver.entity.User;
import com.mars.foodserver.mapper.NotesCommentMapper;
import com.mars.foodserver.response.base.BasePageRequest;
import com.mars.foodserver.response.base.PageInfo;
import com.mars.foodserver.response.vo.FoodCommentVo;
import com.mars.foodserver.response.vo.NotesCommentVo;
import com.mars.foodserver.service.INotesCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.foodserver.service.IUserService;
import com.mars.foodserver.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-09-28
 */
@Service
public class NotesCommentServiceImpl extends ServiceImpl<NotesCommentMapper, NotesComment> implements INotesCommentService {

    @Resource
    private IUserService userService;

    @Override
    public void createComment(Integer userId, Integer noteId, String comment) {
        NotesComment foodComment = new NotesComment();
        foodComment.setUserId(userId);
        foodComment.setNotesId(noteId);
        foodComment.setComment(comment);
        save(foodComment);
    }

    @Override
    public PageInfo<NotesCommentVo> page(BasePageRequest request) {
        IPage<NotesComment> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<NotesComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(NotesComment::getCreateTime);
        List<NotesComment> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            List<NotesCommentVo> list = records.stream().map(x -> {
                NotesCommentVo foodCommentVo = new NotesCommentVo();
                foodCommentVo.setId(x.getId());
                foodCommentVo.setContent(x.getComment());
                foodCommentVo.setCreateTime((x.getCreateTime()));
                User user = userService.selectById(x.getUserId());
                if (Objects.nonNull(user)) {
                    foodCommentVo.setAvatar(user.getAvatar());
                    foodCommentVo.setNickname(user.getNickname());
                }
                return foodCommentVo;
            }).collect(Collectors.toList());
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }

    @Override
    public List<NotesCommentVo> list(Integer noteId) {
        List<NotesComment> foodComments = baseMapper.selectList(new LambdaQueryWrapper<NotesComment>().
                orderByDesc(NotesComment::getCreateTime).
                eq(NotesComment::getNotesId, noteId));
        if (!CollectionUtils.isEmpty(foodComments)) {
            return foodComments.stream().map(x -> {
                NotesCommentVo vo = new NotesCommentVo();
                vo.setContent(x.getComment());
                vo.setId(x.getId());
                User user = userService.selectById(x.getUserId());
                if (Objects.nonNull(user)) {
                    vo.setAvatar(user.getAvatar());
                    vo.setNickname(user.getNickname());
                }
                return vo;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
